package com.business.systemmanage.controller;

import com.business.systemmanage.common.entity.ResponseInfo;
import com.business.systemmanage.common.util.Md5;
import com.business.systemmanage.entity.UserInfo;
import com.business.systemmanage.service.IUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.DateUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Controller
public class LoginController {

    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation(
            value = "登陆",
            notes = "登陆",
            tags = {"登陆"})
    @RequestMapping("/login")
    @ResponseBody
    public ResponseInfo login(@RequestParam String loginName, @RequestParam String password, HttpServletRequest request) {
        try {
            UserInfo login = iUserInfoService.login(loginName, password);
            String userId = login.getId().toString();
            String token = Md5.EncoderByMd5(login.getId().toString());
            redisTemplate.opsForValue().set(token,token);
            //redisTemplate.expire(token, 10,TimeUnit.SECONDS);
            Map<String,String> returnMap = new HashMap<>();
            returnMap.put("token",token );
            return ResponseInfo.generateObjectRes(returnMap);
        } catch (Exception e) {
            return ResponseInfo.generateErrorRes(e.getMessage());
        }
    }


}
